package com.xh.web.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.xh.web.constant.AjaxResult;
import com.xh.web.domain.DTableIndex;
import com.xh.web.service.IDTableIndexService;
import com.xh.web.util.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 索引Controller
 *
 * @author dongxuefei
 * @date 2020-10-16
 */
@RestController
@RequestMapping("/db/index")
public class DTableIndexController extends BaseController
{
    @Autowired
    private IDTableIndexService dTableIndexService;

    /**
     * 查询索引列表
     */
    @GetMapping("/listIndex")
    public TableDataInfo list(DTableIndex dTableIndex)
    {
        startPage();
        List<DTableIndex> list = dTableIndexService.selectDTableIndexList(dTableIndex);
        return getDataTable(list);
    }

    /**
     * 获取树
     */
    @GetMapping("/indexTree")
    public AjaxResult indexTree(DTableIndex dTableIndex)
    {
        return AjaxResult.success(dTableIndexService.getTableTree(dTableIndex.getProjectId()));
    }


    /**
     * 新增索引
     */
    @PostMapping("/insertIndex")
    public AjaxResult add(@RequestBody DTableIndex dTableIndex)
    {
        return toAjax(dTableIndexService.insertDTableIndex(dTableIndex));
    }

    /**
     * 修改索引
     */
    @PutMapping("/updateIndex")
    public AjaxResult edit(@RequestBody DTableIndex dTableIndex)
    {
        return toAjax(dTableIndexService.updateDTableIndex(dTableIndex));
    }

    /**
     * 删除索引
     */
	@DeleteMapping("delIndex/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
        return toAjax(dTableIndexService.deleteDTableIndexById(id));
    }

    /**
     * 预览sql
     */
    @GetMapping("/preview/{IndexId}")
    public AjaxResult preview(@PathVariable("IndexId") String IndexId) throws IOException
    {
        Map<String, String> dataMap = dTableIndexService.previewCode(IndexId);
        return AjaxResult.success(dataMap);
    }
}
